package com.bd1904.db;


import com.bd1904.conf.ConfigurationManager;
import com.bd1904.conf.Constants;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @author old李
    redis的工具类
    入口类就是：Jedis对象，其作用相当于jdbc中的Connection
 */
public class JedisUtil {
    static JedisPool pool;
    static {
        String host = ConfigurationManager.getProperty(Constants.JEDIS_HOST);
        int port = ConfigurationManager.getIntProperty(Constants.JEDIS_PORT);
        JedisPoolConfig jedisConfig = new JedisPoolConfig();
        jedisConfig.setMaxTotal(ConfigurationManager.getIntProperty(Constants.JEDIS_MAX_TOTAL));
        jedisConfig.setMaxIdle(ConfigurationManager.getIntProperty(Constants.JEDIS_MAX_IDLE));
        pool = new JedisPool(jedisConfig, host, port);
    }

    public static Jedis getJedis() {
        return pool.getResource();
    }

    public static void release(Jedis jedis){
        jedis.close();
    }

    public static void main(String[] args) {
        Jedis jedis = getJedis();
        //选择数据库1
        jedis.select(1);
        jedis.set("name", "suiguolongdongqiang");
        release(jedis);
    }
}
